package com.beerandjesus.webapp.sportofkings.handicapper.struts.action;

import com.beerandjesus.sportofkings.handicapper.app.CardAnalyzer;
import com.beerandjesus.sportofkings.handicapper.model.RaceAnalysisResultVO;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.CardVOImpl;
import com.beerandjesus.sportofkings.handicapper.factory.ResultsTableFactory;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;

import java.io.File;
import java.util.List;

/**
 * Date: Jun 1, 2012
 * Time: 1:57:03 PM
 */
public class AnalyzeAction extends ActionSupport {

    private File fileUpload;
    private String fileUploadFileName;
    private String analysisResult;
    private CardAnalyzer cardAnalyzer;
    private ResultsTableFactory resultsTableFactory;

    public String execute() throws Exception {
        CardVOImpl vo = new CardVOImpl();
        vo.setFilePath(getFileUpload().getAbsolutePath());
        List<RaceAnalysisResultVO> results = getCardAnalyzer().analyzeCard(vo);
        String resultString = getResultsTableFactory().getOutputSimpleFormat(results);
        setAnalysisResult(resultString);
        fileUpload.delete();
        return SUCCESS;
    }

    public File getFileUpload() {
        return fileUpload;
    }

    @RequiredFieldValidator(message="Please select a file")
    public void setFileUpload(File fileUpload) {
        this.fileUpload = fileUpload;
    }

    public CardAnalyzer getCardAnalyzer() {
        return cardAnalyzer;
    }

    public void setCardAnalyzer(CardAnalyzer cardAnalyzer) {
        this.cardAnalyzer = cardAnalyzer;
    }

    public String getAnalysisResult() {
        return analysisResult;
    }

    public void setAnalysisResult(String analysisResult) {
        this.analysisResult = analysisResult;
    }

    public String getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(String fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

    public ResultsTableFactory getResultsTableFactory() {
        return resultsTableFactory;
    }

    public void setResultsTableFactory(ResultsTableFactory resultsTableFactory) {
        this.resultsTableFactory = resultsTableFactory;
    }
}
